#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-04-19 12:33
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：返回制样结果

class mainRun(object):
    """docstring for main"""

    def __init__(self):
        super(mainRun, self).__init__()
        self.log = main.log
        self.cics = main.conn.get("CICS", {}).get('connect')
        self.ics = main.conn.get("ICS", {}).get('connect')

    def query_main_table(self):
        sql = "SELECT * FROM T_550_ZYCZP_MASTE WHERE F_CZPZT='00000005' AND F_SYN_FLAG=0 AND F_KPSJ>(SYSDATE-3)"
        data = self.ics.loadSql(sql, True)
        return data

    def qurey_sub_table(self, pgid):
        sql = "SELECT * FROM T_550_ZYCZP_DETAIL WHERE F_PID='{}' AND F_ZHUANGTAI='0202' AND F_SYN_FLAG=1".format(pgid)
        data = self.ics.loadSql(sql, True)
        return data

    def upload_main_table(self, data):
        czpzt = data.get('F_CZPZT')
        czpjg = data.get('F_CZPJG', 1)
        czpkssj = data.get('F_CZPKSSJ')
        czpjssj = data.get('F_CZPJSSJ')
        gid = data.get('F_ID')
        sql = ("UPDATE T_RL_CZPMAIN SET CZPZT='{}',CZPJG='{}',CZPKSSJ=TO_DATE('{}','yyyy-mm-dd hh24:mi:ss'),"
               "CZPJSSJ=TO_DATE('{}','yyyy-mm-dd hh24:mi:ss'),UPDTIME=SYSDATE,ISREADCICS=0 WHERE GID='{}'").format(czpzt, czpjg,
                                                                                                     czpkssj, czpjssj,
                                                                                                     gid)
        self.log.info("上传操作票结果:{}".format(sql))
        res = self.cics.execSql(sql)
        return res

    def update_flag(self, gid):
        sql = "UPDATE T_550_ZYCZP_MASTE SET F_SYN_FLAG=1 WHERE F_ID='{}'".format(gid)
        self.log.info("回写上传操作票结果标记:{}".format(sql))
        res = self.ics.execSql(sql)
        return res

    def work(self):
        data = self.query_main_table()
        if not data:
            return
        for i, row in enumerate(data):
            pgid = row.get('F_ID')
            czpbh = row.get('F_CZPBH')
            sub = self.qurey_sub_table(pgid)
            if not sub or len(sub) < 4:
                self.log.info('操作票:{},子表数据还未回写上传完成,等待子表数据全部回写上传完成....'.format(czpbh))
                continue
            res = self.upload_main_table(row)
            if not res:
                self.log.info("操作票:{},主表结果上传失败...".format(czpbh))
                continue
            self.log.info("操作票:{},主表结果上传结果完成...".format(czpbh))
            res1 = self.update_flag(pgid)
            if not res1:
                self.log.info("上传完成,回写标记失败...")
                continue
            self.log.info('操作票:{},流程结束....'.format(czpbh))


app = mainRun()
app.work()